package simpleTaskDispatcher.model.tasks.impl;

import java.util.concurrent.Semaphore;

import simpleTaskDispatcher.model.enums.DispatchingEvent;
import simpleTaskDispatcher.model.tasks.ITaskAllowedAfterTaskQueueClose;

public class ReleaseWorkerTask extends ContextFreeTask implements ITaskAllowedAfterTaskQueueClose {

	private final Semaphore releasedSemaphore;
	
	private final DispatchingEvent event;
	
	private final boolean releaseAll;

	public ReleaseWorkerTask(boolean releaseAll, DispatchingEvent event) {
		this.releasedSemaphore = null;
		this.event = event;
		this.releaseAll = releaseAll;
		this.setWorkValue(0);
	}
	
	public ReleaseWorkerTask(boolean releaseAll, DispatchingEvent event, Semaphore releasedSemaphore) {
		this.releasedSemaphore = releasedSemaphore;
		this.event = event;
		this.releaseAll = releaseAll;
		this.setWorkValue(0);
	}
	
	public void execute() throws InterruptedException {
		if (releasedSemaphore != null) {
			releasedSemaphore.release();
		}
	}

	public Semaphore getReleasedSemaphore() {
		return releasedSemaphore;
	}

	public DispatchingEvent getEvent() {
		return event;
	}
	
	public boolean isReleaseAll() {
		return releaseAll;
	}
}
